package com.blog.controller;

import com.blog.exception.ExceptionEnum;
import com.blog.pojo.ResultResponse;
import com.blog.pojo.Tag;
import com.blog.service.ArticleTagService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 文章标签关联表管理控制层
@Slf4j
@RequestMapping("/articletags")
@RestController
public class ArticleTagController {

    @Autowired
    private ArticleTagService articleTagService;

    // 根据文章id查询标签
    @GetMapping("/{id}")
    public ResultResponse listByArticleId(@PathVariable Integer id) {
        log.info("查看文章的所有标签，文章id: {}", id);
        List<Tag> tagList = articleTagService.listByArticleId(id);
        return ResultResponse.success(tagList);
    }

    // 删除文章对应的所有标签
    @DeleteMapping("/{articleId}")
    public ResultResponse delete(@PathVariable Integer articleId){
        log.info ("删除文章标签，文章id: {}", articleId);
        articleTagService.deleteByArticleId(articleId);
        return ResultResponse.success();
    }

    @PostMapping("/{articleId}/{tagIds}")
    public ResultResponse add(@PathVariable Integer articleId, @PathVariable List<Integer> tagIds) {
        log.info("已发布文章添加标签，文章id: {}，标签id: {}", articleId, tagIds);
        if(tagIds == null) {
            return ResultResponse.error(ExceptionEnum.EMPTY_TAG);
        }
        articleTagService.add(articleId, tagIds);
        return ResultResponse.success();
    }
}
